|
1
|
|
|
var ExtensibleData = require('./ExtensibleData'), |
|
2
|
|
|
ResourceReference = require('./ResourceReference'), |
|
3
|
|
|
OnlineAccount = require('./OnlineAccount'), |
|
4
|
|
|
Address = require('./Address'), |
|
5
|
|
|
Identifiers = require('./Identifiers'), |
|
6
|
|
|
TextValue = require('./TextValue'), |
|
7
|
|
|
utils = require('./utils'); |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Someone or something that curates genealogical data, such as a genealogical |
|
11
|
|
|
* researcher, user of software, or organization. |
|
12
|
|
|
* |
|
13
|
|
|
* @constructor |
|
14
|
|
|
* @param {Object} [json] |
|
|
|
|
|
|
15
|
|
|
*/ |
|
16
|
|
|
var Agent = function(json){ |
|
17
|
|
|
|
|
18
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
|
19
|
|
|
if(!(this instanceof Agent)){ |
|
20
|
|
|
return new Agent(json); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
|
24
|
|
|
if(Agent.isInstance(json)){ |
|
25
|
|
|
return json; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
ExtensibleData.call(this, json); |
|
29
|
|
|
|
|
30
|
|
|
if(json){ |
|
|
|
|
|
|
31
|
|
|
this.setIdentifiers(json.identifiers); |
|
32
|
|
|
this.setNames(json.names); |
|
33
|
|
|
this.setHomepage(json.homepage); |
|
34
|
|
|
this.setOpenid(json.openid); |
|
35
|
|
|
this.setAccounts(json.accounts); |
|
36
|
|
|
this.setEmails(json.emails); |
|
37
|
|
|
this.setPhones(json.phones); |
|
38
|
|
|
this.setAddresses(json.addresses); |
|
39
|
|
|
this.setPerson(json.person); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
}; |
|
42
|
|
|
|
|
43
|
|
|
Agent.prototype = Object.create(ExtensibleData.prototype); |
|
44
|
|
|
|
|
45
|
|
|
Agent._gedxClass = Agent.prototype._gedxClass = 'GedcomX.Agent'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Check whether the given object is an instance of this class. |
|
49
|
|
|
* |
|
50
|
|
|
* @param {Object} obj |
|
51
|
|
|
* @returns {Boolean} |
|
52
|
|
|
*/ |
|
53
|
|
|
Agent.isInstance = function(obj){ |
|
54
|
|
|
return utils.isInstance(obj, this._gedxClass); |
|
55
|
|
|
}; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the identifiers |
|
59
|
|
|
* |
|
60
|
|
|
* @returns {Identifiers} |
|
61
|
|
|
*/ |
|
62
|
|
|
Agent.prototype.getIdentifiers = function(){ |
|
63
|
|
|
return this.identifiers; |
|
64
|
|
|
}; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set the identifiers |
|
68
|
|
|
* |
|
69
|
|
|
* @param {Identifiers} identifiers |
|
70
|
|
|
* @returns {Agent} |
|
71
|
|
|
*/ |
|
72
|
|
|
Agent.prototype.setIdentifiers = function(identifiers){ |
|
73
|
|
|
if(identifiers){ |
|
74
|
|
|
this.identifiers = Identifiers(identifiers); |
|
75
|
|
|
} |
|
76
|
|
|
return this; |
|
77
|
|
|
}; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get the names |
|
81
|
|
|
* |
|
82
|
|
|
* @returns {TextValue[]} |
|
83
|
|
|
*/ |
|
84
|
|
|
Agent.prototype.getNames = function(){ |
|
85
|
|
|
return this.names || []; |
|
86
|
|
|
}; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Set the names |
|
90
|
|
|
* |
|
91
|
|
|
* @param {TextValue[]|Object[]} names |
|
92
|
|
|
* @returns {Agent} |
|
93
|
|
|
*/ |
|
94
|
|
|
Agent.prototype.setNames = function(names){ |
|
95
|
|
|
return this._setArray(names, 'names', 'addName'); |
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Add a name |
|
100
|
|
|
* |
|
101
|
|
|
* @param {TextValue|Object} name |
|
102
|
|
|
* @return {Agent} |
|
103
|
|
|
*/ |
|
104
|
|
|
Agent.prototype.addName = function(name){ |
|
105
|
|
|
return this._arrayPush(name, 'names', TextValue); |
|
106
|
|
|
}; |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the home page |
|
110
|
|
|
* |
|
111
|
|
|
* @returns {ResourceReference} |
|
112
|
|
|
*/ |
|
113
|
|
|
Agent.prototype.getHomepage = function(){ |
|
114
|
|
|
return this.homepage; |
|
115
|
|
|
}; |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set the home page |
|
119
|
|
|
* |
|
120
|
|
|
* @param {ResourceReference|Object} homepage |
|
121
|
|
|
* @returns {Agent} |
|
122
|
|
|
*/ |
|
123
|
|
|
Agent.prototype.setHomepage = function(homepage){ |
|
124
|
|
|
if(homepage){ |
|
125
|
|
|
this.homepage = ResourceReference(homepage); |
|
126
|
|
|
} |
|
127
|
|
|
return this; |
|
128
|
|
|
}; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get the openid |
|
132
|
|
|
* |
|
133
|
|
|
* @returns {ResourceReference} |
|
134
|
|
|
*/ |
|
135
|
|
|
Agent.prototype.getOpenid = function(){ |
|
136
|
|
|
return this.openid; |
|
137
|
|
|
}; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set the openid |
|
141
|
|
|
* |
|
142
|
|
|
* @params {ResourceReference} openid |
|
143
|
|
|
* @returns {Agent} |
|
144
|
|
|
*/ |
|
145
|
|
|
Agent.prototype.setOpenid = function(openid){ |
|
146
|
|
|
if(openid){ |
|
147
|
|
|
this.openid = ResourceReference(openid); |
|
148
|
|
|
} |
|
149
|
|
|
return this; |
|
150
|
|
|
}; |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get the accounts |
|
154
|
|
|
* |
|
155
|
|
|
* @returns {OnlineAccount[]} |
|
156
|
|
|
*/ |
|
157
|
|
|
Agent.prototype.getAccounts = function(){ |
|
158
|
|
|
return this.accounts || []; |
|
159
|
|
|
}; |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Set the accounts |
|
163
|
|
|
* |
|
164
|
|
|
* @param {OnlineAccount[]|Object[]} accounts |
|
165
|
|
|
* @returns {Agent} |
|
166
|
|
|
*/ |
|
167
|
|
|
Agent.prototype.setAccounts = function(accounts){ |
|
168
|
|
|
return this._setArray(accounts, 'accounts', 'addAccount'); |
|
169
|
|
|
}; |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Add an account |
|
173
|
|
|
* |
|
174
|
|
|
* @param {OnlineAccount|Object} account |
|
175
|
|
|
* @returns {Agent} |
|
176
|
|
|
*/ |
|
177
|
|
|
Agent.prototype.addAccount = function(account){ |
|
178
|
|
|
return this._arrayPush(account, 'accounts', OnlineAccount); |
|
179
|
|
|
}; |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the emails |
|
183
|
|
|
* |
|
184
|
|
|
* @returns {ResourceReference[]} |
|
185
|
|
|
*/ |
|
186
|
|
|
Agent.prototype.getEmails= function(){ |
|
187
|
|
|
return this.emails || []; |
|
188
|
|
|
}; |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Set the emails |
|
192
|
|
|
* |
|
193
|
|
|
* @param {ResourceReference[]|Object[]} emails |
|
194
|
|
|
* @returns {Agent} |
|
195
|
|
|
*/ |
|
196
|
|
|
Agent.prototype.setEmails = function(emails){ |
|
197
|
|
|
return this._setArray(emails, 'emails', 'addEmail'); |
|
198
|
|
|
}; |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Add an email |
|
202
|
|
|
* |
|
203
|
|
|
* @param {ResourceReference|Object} email |
|
204
|
|
|
* @returns {Agent} |
|
205
|
|
|
*/ |
|
206
|
|
|
Agent.prototype.addEmail = function(email){ |
|
207
|
|
|
return this._arrayPush(email, 'emails', ResourceReference); |
|
208
|
|
|
}; |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Get the phones |
|
212
|
|
|
* |
|
213
|
|
|
* @returns {ResourceReference[]} |
|
214
|
|
|
*/ |
|
215
|
|
|
Agent.prototype.getPhones = function(){ |
|
216
|
|
|
return this.phones || []; |
|
217
|
|
|
}; |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Set the phones |
|
221
|
|
|
* |
|
222
|
|
|
* @param {ResourceReference[]|Object[]} phones |
|
223
|
|
|
* @returns {Agent} |
|
224
|
|
|
*/ |
|
225
|
|
|
Agent.prototype.setPhones = function(phones){ |
|
226
|
|
|
return this._setArray(phones, 'phones', 'addPhone'); |
|
227
|
|
|
}; |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Add a phone |
|
231
|
|
|
* |
|
232
|
|
|
* @param {ResourceReference|Object} phone |
|
233
|
|
|
* @returns {Agent} |
|
234
|
|
|
*/ |
|
235
|
|
|
Agent.prototype.addPhone = function(phone){ |
|
236
|
|
|
return this._arrayPush(phone, 'phones', ResourceReference); |
|
237
|
|
|
}; |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Get the addresses |
|
241
|
|
|
* |
|
242
|
|
|
* @returns {Address[]} |
|
243
|
|
|
*/ |
|
244
|
|
|
Agent.prototype.getAddresses = function(){ |
|
245
|
|
|
return this.addresses || []; |
|
246
|
|
|
}; |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Set the addresses |
|
250
|
|
|
* |
|
251
|
|
|
* @param {Address[]|Object[]} addresses |
|
252
|
|
|
* @returns {Agent} |
|
253
|
|
|
*/ |
|
254
|
|
|
Agent.prototype.setAddresses = function(addresses){ |
|
255
|
|
|
return this._setArray(addresses, 'addresses', 'addAddress'); |
|
256
|
|
|
}; |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Add an address |
|
260
|
|
|
* |
|
261
|
|
|
* @param {Address|Object} address |
|
262
|
|
|
* @returns {Agent} |
|
263
|
|
|
*/ |
|
264
|
|
|
Agent.prototype.addAddress = function(address){ |
|
265
|
|
|
return this._arrayPush(address, 'addresses', Address); |
|
266
|
|
|
}; |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Get the person reference |
|
270
|
|
|
* |
|
271
|
|
|
* @returns {ResourceReference} |
|
272
|
|
|
*/ |
|
273
|
|
|
Agent.prototype.getPerson = function(){ |
|
274
|
|
|
return this.person; |
|
275
|
|
|
}; |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Set the person reference |
|
279
|
|
|
* |
|
280
|
|
|
* @param {ResourceReference} person |
|
281
|
|
|
* @returns {Agent} |
|
282
|
|
|
*/ |
|
283
|
|
|
Agent.prototype.setPerson = function(person){ |
|
284
|
|
|
if(person){ |
|
285
|
|
|
this.person = ResourceReference(person); |
|
286
|
|
|
} |
|
287
|
|
|
return this; |
|
288
|
|
|
}; |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Export the object as JSON |
|
292
|
|
|
* |
|
293
|
|
|
* @return {Object} JSON object |
|
294
|
|
|
*/ |
|
295
|
|
|
Agent.prototype.toJSON = function(){ |
|
296
|
|
|
return this._toJSON(ExtensibleData, [ |
|
297
|
|
|
'identifiers', |
|
298
|
|
|
'names', |
|
299
|
|
|
'homepage', |
|
300
|
|
|
'openid', |
|
301
|
|
|
'accounts', |
|
302
|
|
|
'emails', |
|
303
|
|
|
'phones', |
|
304
|
|
|
'addresses', |
|
305
|
|
|
'person' |
|
306
|
|
|
]); |
|
307
|
|
|
}; |
|
308
|
|
|
|
|
309
|
|
|
module.exports = Agent; |